#!/bin/sh
# PCP QA Test No. 1491
# Basic pmcheck exercising
#
# Copyright (c) 2024 Ken McDonell.  All Rights Reserved.
#

if [ $# -eq 0 ]
then
    seq=`basename $0`
    echo "QA output created by $seq"
else
    # use $seq from caller, unless not set
    [ -n "$seq" ] || seq=`basename $0`
    echo "QA output created by `basename $0` $*"
fi

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check


_cleanup()
{
    cd $here
    $sudo rm -rf $tmp $tmp.*
    # make sure pmcd is really running ...
    #
    if [ ! -f $PCP_RUN_DIR/pmcd.pid ]
    then
	$sudo _service pmcd restart >>$seq_full
	_wait_for_pmcd
    fi
    # and primary pmlogger is back in the land of the living
    #
    _wait_for_pmlogger
}

status=0	# success is the default!
trap "_cleanup; exit \$status" 0 1 2 3 15

_filter()
{
    sed \
	-e "s@$tmp@TMP@g" \
	-e "s@`basename $tmp`@TMP@g" \
    # end
}

_filter_states()
{
    sed \
	-e 's/  *$//' \
	-e 's/ active$/ OK/' \
	-e '/^pmproxy /{
s/inactive/OK/
s/could be activated/OK/
}' \
	-e '/^pmie /{
s/inactive/OK/
s/could be activated/OK/
}' \
	-e '/^pmda-overhead /{
s/inactive/OK/
s/could be activated/OK/
s/cannot be activated/OK/
s/measure overhead of PCP components/.../
s/not currently available for .*/.../
}' \
	-e '/^pmda-postgresql /{
s/PostgreSQL/postgresql/g
s/inactive/OK/
s/could be activated/OK/
s/cannot be activated/OK/
s/  No systemd unit file installed//
s/  systemd.s postgresql.service unit is not installed//
s@  /etc/init.d/postgresql is not installed@@
s/not currently available .*/metrics from postgresql/
}' \
	-e '/^pmda-redis /{
s/Redis/redis/g
s/inactive/OK/
s/could be activated/OK/
s/cannot be activated/OK/
s/  systemd.s redis.service unit is not installed//
s/  systemd.s redis.service unit has failed//
s/  systemd.s redis-server.service unit is not installed//
s/  systemd.s redis-server.service unit has failed//
s@  /etc/init.d/redis-server is not installed@@
s/not currently available .*/metrics from redis-server(1)/
}' \
	-e '/^pmda-uwsgi /{
s/uWSGI/uwsgi/g
s/inactive/OK/
s/could be activated/OK/
s/cannot be activated/OK/
s/not currently available .*/metrics from uwsgi servers/
}' \
	-e '/^zeroconf /{
s/inactive/OK/
s/could be activated/OK/
s/cannot be activated/OK/
s/Package$/& .../
s/Package - not currently available for .*/Package .../
}' \
    #end
}

# real QA test starts here

echo "basic stuff ..."
# expect most things to be active, but some are optional ...
#
# some services need to run as root, even for "status"
# requests, e.g. redis-server without systemd
#
echo '-- no args --'
$sudo $PCP_BINADM_DIR/pmcheck 2>&1 | _filter_states
echo '-- -l --'
$PCP_BINADM_DIR/pmcheck -l
echo '-- -lv --'
$PCP_BINADM_DIR/pmcheck -lv | _filter_states
echo '-- -s --'
$sudo $PCP_BINADM_DIR/pmcheck -s 2>&1 | _filter_states

touch $tmp.pmcheck
echo
echo "=== usage ==="
echo '-- --help --'
$PCP_BINADM_DIR/pmcheck --help
echo '-- -l + -c --'
$PCP_BINADM_DIR/pmcheck -l -c /bin/echo
echo '-- -l + component arg --'
$PCP_BINADM_DIR/pmcheck -l pmcd
echo '-- -c does not exist --'
$PCP_BINADM_DIR/pmcheck -c /no.such.file -s
echo '-- -c not executable --'
$PCP_BINADM_DIR/pmcheck -c $tmp.pmcheck -s 2>&1 | _filter
echo '-- -c + component arg --'
$PCP_BINADM_DIR/pmcheck -c /bin/echo -s pmcd
echo '-- component arg + no action opt --'
$PCP_BINADM_DIR/pmcheck pmcd
echo '-- -n without -a or -d --'
$PCP_BINADM_DIR/pmcheck -n -s pmcd
echo '-- -c + no action opt --'
$PCP_BINADM_DIR/pmcheck -c /bin/echo
echo '-- multiple actions --'
$PCP_BINADM_DIR/pmcheck -a -d pmcd
$PCP_BINADM_DIR/pmcheck -l -s
echo '-- bad option --'
$PCP_BINADM_DIR/pmcheck -f -o -o
$PCP_BINADM_DIR/pmcheck -foo

echo
echo "=== parsing and arg hand-off to component script ==="
cat <<'End-of-File' >$tmp.pmcheck
#!/bin/sh
echo "component called, $# args: $*"
exit 0
End-of-File
chmod 755 $tmp.pmcheck
echo '-- -c ... -s --'
$PCP_BINADM_DIR/pmcheck -c $tmp.pmcheck -s 2>&1 | _filter
# no action/state output for this one
echo '-- -c ... -nav --'
$PCP_BINADM_DIR/pmcheck -c $tmp.pmcheck -nav 2>&1 | _filter
cat <<'End-of-File' >$tmp.pmcheck
#!/bin/sh
. $PCP_DIR/etc/pcp.env || exit 1
. $PCP_SHARE_DIR/lib/checkproc.sh
_do_args "$@"
if $lflag
then
    [ $verbose -gt 0 ] && echo I am here for QA 1491
elif $sflag
then
    [ $verbose -gt 0 ] && echo some state explanation
    status=$RC
elif $aflag
then
    if $show_me
    then
	echo "# some-magic-activation-command $component"
    else
	: some-magic-activation-command
    fi
    [ $verbose -gt 0 ] && echo some activation explanation
    status=$RC
elif $dflag
then
    if $show_me
    then
	echo "# some-magic-deactivation-command $component"
    else
	: some-magic-deactivation-command
    fi
    [ $verbose -gt 0 ] && echo some deactivation explanation
    status=$RC
fi
End-of-File
echo "-- -c ... -lv --"
$PCP_BINADM_DIR/pmcheck -c $tmp.pmcheck -lv 2>&1 | _filter
for rc in 0 1 2
do
    echo "-- RC=$rc -c ... -s --"
    RC=$rc $PCP_BINADM_DIR/pmcheck -c $tmp.pmcheck -s 2>&1 | _filter
done
for rc in 0 1
do
    for opt in '-a' '-av' '-an' '-anv' '-d' '-dv' '-dn' '-dnv'
    do
	echo "-- RC=$rc -c ... $opt --"
	RC=$rc $PCP_BINADM_DIR/pmcheck -c $tmp.pmcheck $opt 2>&1 | _filter
    done
done

echo
echo "=== glob expansion ==="
echo '-- -s pm[cl]* --'
$PCP_BINADM_DIR/pmcheck -s 'pm[cl]*'

echo
echo "=== actions ==="
echo '-- -d --'
$sudo $PCP_BINADM_DIR/pmcheck -d pmcd
_wait_for_pmcd_stop || _exit 1
if [ -f $PCP_RUN_DIR/pmcd.pid ]
then
    echo "$seq: Botch: $PCP_RUN_DIR/pmcd.pid => `cat $PCP_RUN_DIR/pmcd.pid`"
else
    echo "$seq: pmcd stopped"
fi
$PCP_BINADM_DIR/pmcheck -s pmcd
echo '-- and -d again with -v --'
$sudo $PCP_BINADM_DIR/pmcheck -v -d pmcd 2>&1 \
| sed \
    -e '/^  *already /d' \
    -e 's/ stopped via .*/ STOPPED via .../' \
    -e 's/ disabled via .*/ STOPPED via .../' \
# end

echo '-- -a --'
$sudo $PCP_BINADM_DIR/pmcheck -a pmcd
_wait_for_pmcd || _exit 1
if [ -f $PCP_RUN_DIR/pmcd.pid ]
then
    echo "$seq: pmcd started"
else
    echo "$seq: Botch: $PCP_RUN_DIR/pmcd.pid missing"
fi
$PCP_BINADM_DIR/pmcheck -s pmcd
echo '-- and -a again with -v --'
$PCP_BINADM_DIR/pmcheck -v -a pmcd 2>&1 \
| sed \
    -e '/^  *already /d' \
    -e 's/ enabled via .*/ STARTED via .../' \
    -e 's/ started via .*/ STARTED via .../' \
# end

# success, all done
exit
